<?php
include('functions.inc.php');
$link = db_connect();

header("Content-type: text/html; charset=UTF-8");

$what = '';
if (isset($_POST['articles_id']) && $_POST['articles_id'] != '[ArticlesId]' && trim($_POST['articles_id']) != '') {
	$articles_id = explode(' ', $_POST['articles_id']);
	$articles_id = implode($articles_id, ',');
	$where = "a.id IN ($articles_id)";
} else if (isset($_POST['search_query']) && $_POST['search_query'] != '[Search]') {
	$where = "(1";
	$search_query = explode(' ', $_POST['search_query']);
	foreach ($search_query as $word) {
		$where .= " AND (a.title LIKE '%$word%' OR a.link LIKE '%$word%' OR a.description LIKE '%$word%')";
	}
	$where .= ")";
} else if (!isset($_GET['what'])) {
	$where = "(state LIKE '%new%' or state LIKE '%unread%')";
	$what = "new or unread";
} else if ($_GET['what'] == 'unread') {
	$where = "state LIKE '%unread%'";
	$what = "unread";
} else if ($_GET['what'] == 'starred') {
	$where = "state LIKE '%starred%'";
	$what = "starred";
} else if ($_GET['what'] == 'new') {
	$where = "state LIKE '%new%'";
	$what = "new";
} else if ($_GET['what'] == 'all') {
	$where = "1";
	$what = "all";
} else {
	$where = "(state LIKE '%new%' or state LIKE '%unread%')";
	$what = "new or unread";
}

$addUrl = '';
if (isset($_GET['sub'])) {
	$where .= " AND subId = " . mysql_real_escape_string($_GET['sub']);
	$addUrl = '?sub='.$_GET['sub'].'&';
}
if (isset($_GET['not_sub'])) {
	$where .= " AND subId <> " . mysql_real_escape_string($_GET['not_sub']);
	$addUrl = '?not_sub='.$_GET['not_sub'].'&';
}

// DB Migration
include('create_tables.inc.php');
include('db-migration.inc.php');

$articles_query = "SELECT a.id, a.title, a.state, s.title AS subTitle, description FROM articles a LEFT JOIN subscriptions s ON (a.subId = s.id) WHERE $where ORDER BY pubDate ASC";
$result = mysql_query($articles_query, $link);
if (mysql_errno()) {
	die("Can't load articles: " . mysql_error() . "\n");
}

$articles = array();
$js = '';
while ($article = mysql_fetch_array($result)) {
	if (filtered_article($article['title'],$article['description'])) {
		$js .= "action('new','remove','".$article['id']."');\n";
		continue;
	}
	$articles[] = $article;
}
mysql_close($link);

if ($config['override_arrow_keys']) {
	$js .= "override_arrow_keys = true;\n";
}

$numRows = sizeof($articles);
?>
<html>
<head>
	<title>GobbleRSS</title>
	<link href="css/gob.css" type="text/css" rel="stylesheet">
	<script type="text/javascript" src="js/prototype.js"></script>
	<script type="text/javascript" src="js/sexybox.js"></script>
	<script type="text/javascript">var numRows = <?php echo $numRows ?>;var gob_url='<?php echo $config['URI'] ?>'</script>
	<script type="text/javascript" src="js/gob.js"></script>
	<script type="text/javascript">
		var MAX_CACHE_SIZE = 500*1024; // 500kB
		var cacheSize = 0
		var articlesIdCache = new Array();
		var articlesCache = new Array();
		var currentArtId = 0;
		window.onload = function() { resizePanes(); loadArticle(1); }
		window.onresize = resizePanes;
		window.document.addEventListener("keydown", keyDown, true);
		window.document.addEventListener("keypress", keyPress, true);
		var readingMode = false;
		<?php print $js; ?>
	</script>
</head>

<body>
<div id="sexyBG"></div><div id="sexyBOX"></div>

<div style="padding-top: 10px; float: right">
	<form method="post" action="index.php">
		<?php include ('menu.inc.php') ?>
		&nbsp;|&nbsp; <span id="grab_latest"><a href="grabLatest" onclick="return grabLatest('<?php echo $config['passphrase'] ?>')">Grab Latest</a></span>
		&nbsp;|&nbsp; <input type="text" name="search_query" id="search_query" value="[Search]" onclick="if(this.value=='[Search]')this.value='';searching=true" style="text-align: center" size="10" />
		&nbsp;|&nbsp; Read &nbsp;<input type="text" name="articles_id" id="articles_id" value="[ArticlesId]" onclick="if(this.value=='[ArticlesId]')this.value=''" style="text-align: center" size="10" onfocus="in_articles_id=true" onblur="in_articles_id=false" />
		<input style="display: none" type="submit" />
	</form>
</div>
<h1>Reading (<span id="numArticles"><?php echo $numRows ?></span>) <?php echo $what ?> articles</h1>

<div id="articlesPane">
	<div id="scrolling">
		<div id="art_0">&nbsp;<br/>&nbsp;</div>
		<div id="chan_0">&nbsp;</div>
<?php
$i=0;
foreach ($articles as $article) {
	$i++;
	$id = $article['id'];
	$title = $article['title'];
	$subTitle = $article['subTitle'];
	$state = $article['state'];
	echo "		<div id=\"art_$i\" class=\"aTitle " . implode(explode(',',$state), ' ') . "\" onclick=\"loadArticle($i)\">$title</div>\n";
	echo "		<div id=\"chan_$i\" class=\"channel\" onclick=\"loadArticle($i)\">$subTitle<input type=\"hidden\" id=\"id_$i\" value=\"$id\" /></div>\n";
}
?>
	</div>
</div>
<div id="readingPane"></div>
<div id="shortcutsPane">
	Shortcuts:&nbsp;
	<strong>j</strong> - <a href="scrollDn" onclick="return scrollDn()">down</a>
	&nbsp;|&nbsp; <strong>k</strong> - <a href="scrollUp" onclick="return scrollUp()">up</a>
	<!--&nbsp;|&nbsp; <strong>h</strong> - <a href="goHome" onclick="return loadArticle(1)">top (home)</a>-->
	&nbsp;|&nbsp; <strong>u</strong> - <a href="keepUnread" onclick="return keepUnreadCurrent()">mark unread</a>
	&nbsp;|&nbsp; <strong>s</strong> - <a href="star" onclick="return starCurrent()">star</a>
	&nbsp;|&nbsp; <strong>v/o/space</strong> - <a href="open" onclick="openCurentLink(); window.open($('currentLink').href); return false">view original article</a>
</div>
<script type="text/javascript">readingMode = true;</script>
<div id="prefetchPane" />
</body>
</html>
